package defaultPackage;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class Input implements KeyListener 
{
	MainClass main;
	public Input(MainClass main)
	{
		this.main = main;
	}
	
	 public void keyPressed (KeyEvent e)
	    {	
	        if (e.getKeyCode() == KeyEvent.VK_ENTER)
	        {
	        	if (main.cursor.isActive() && main.dmenu.isActive())
	        	{
	        		dmenuAction();
	        	}
	        	else if (main.cmenu.isActive())
	        	{
	        		cmenuAction();
	        	}
	        	else if(main.cursor.isActive() && !main.cmenu.isActive()&& !main.dmenu.isActive() && !main.grid.tiles[main.cursor.getX()/10][main.cursor.getY()/10].type.getSolid())
	        	{
	        		personAction();
	        	}
	        	if (main.cursor.isActive() && !main.cmenu.isActive()&& !main.dmenu.isActive())
	        	{
		        	doorAction();
	        	}
	        }
	        
	        else if (e.getKeyCode() == KeyEvent.VK_I && main.dmenu.isActive() && main.cursor.isActive())
	        {
	        	main.designation.setDesignationType(tileType.Interior);
	        }
	        
	        else if (e.getKeyCode() == KeyEvent.VK_C && !main.dmenu.isActive() && !main.cursor.isActive())
	        {
	        	main.cursor.setActive(true);
	        	main.dmenu.setActive(false);
	        	main.designation.setDesignating(false);
	        	main.designation.setFollowing(false);
	        	if (!main.cmenu.isActive())
	        		main.cmenu.setActive(true);
	        }
	        
	        else if (e.getKeyCode() == KeyEvent.VK_R && main.cursor.isActive() && main.cmenu.isActive())
	        {
	        	main.cursor.setObjectType(ObjectType.ResourceGatherer);
	        }
	        
	        
	        
	        
	        else if (e.getKeyCode() == KeyEvent.VK_D)
	        {
	        	if (main.cmenu.isActive())
	        	{
		        	main.cursor.setObjectType(ObjectType.Door);
	        	}
	        	else if (!main.cursor.isActive())
	        	{
	        		if (!main.dmenu.isActive() && !main.cmenu.isActive())
	        		{
	        			main.cursor.setActive(true);
	        			main.dmenu.setActive(true);
	        		}
	        	}
	        }
	        
	        else if (e.getKeyCode() == KeyEvent.VK_L && main.cmenu.isActive())
	        {
	        	main.cursor.setObjectType(ObjectType.LifeSupport);
	        }
	        
	        else if (e.getKeyCode() == KeyEvent.VK_H && main.dmenu.isActive())
	        {
	        	main.designation.setDesignationType(tileType.Hull);
	        }
	        
	        else if (e.getKeyCode() == KeyEvent.VK_X && main.dmenu.isActive())
	        {
	        	main.designation.setDesignationType(tileType.Space);
	        }
	        
	        else if (e.getKeyCode() == KeyEvent.VK_X && main.cmenu.isActive())
	        {
	        	main.cursor.setObjectType(null);
	        }

	        else if (e.getKeyCode() == KeyEvent.VK_A && main.cmenu.isActive())
	        {
	        	main.cursor.setObjectType(ObjectType.Airlock);
	        }
	        
	        else if (e.getKeyCode() == KeyEvent.VK_SHIFT)
	        {
	        	if (main.cursor.isActive() && !main.dmenu.isActive() && !main.cmenu.isActive())
	        	{
	        		main.cursor.setActive(false);
	        		main.keymenu.setActive(true);
	        		main.cumenu.setActive(false);
	        			
	        	}
	        	else if(!main.dmenu.isActive() && !main.cmenu.isActive())
	        	{
		        	main.cursor.setActive(true);
		        	main.cursor.setAccelerated(false);
		        	main.keymenu.setActive(false);
		        	main.designation.setDesignating(false);
	        		main.designation.setFollowing(false);
	        		main.dmenu.setActive(false);
	        		main.cmenu.setActive(false);
	        		main.cumenu.setActive(true);
	        	}
	        }
	        
	        
	        
	        if (e.getKeyCode() == KeyEvent.VK_LEFT && main.cursor.isActive())
	        {
	        	main.cursor.cursorLeft(true);
	        }
	        if (e.getKeyCode() == KeyEvent.VK_RIGHT && main.cursor.isActive())
	        {
	        	main.cursor.cursorRight(true);
	        }
	        if (e.getKeyCode() == KeyEvent.VK_UP && main.cursor.isActive())
	        {
	        	main.cursor.cursorUP(true);
	        }
	        if (e.getKeyCode() == KeyEvent.VK_DOWN && main.cursor.isActive())
	        {
	        	main.cursor.cursorDown(true);
	        }
	        
	        
	        
	        
	        else if (e.getKeyCode() == KeyEvent.VK_LEFT && main.cursor.isActive() == false)
	        {
	        	main.camera.cameraLeft();
	        	main.cursor.cursorLeft(true);
	        }
	        else if (e.getKeyCode() == KeyEvent.VK_RIGHT && main.cursor.isActive() == false)
	        {
	        	main.camera.cameraRight();
	        	main.cursor.cursorRight(true);
	        }
	        else if (e.getKeyCode() == KeyEvent.VK_UP && main.cursor.isActive() == false)
	        {
	        	main.camera.cameraUp();
	        	main.cursor.cursorUP(true);
	        }
	        else if (e.getKeyCode() == KeyEvent.VK_DOWN && main.cursor.isActive() == false)
	        {
	        	main.camera.cameraDown();
	        	main.cursor.cursorDown(true);
	        }
	        if (e.getKeyCode() == KeyEvent.VK_SPACE && main.cursor.isActive())
	        {
	        	main.cursor.setAccelerated(true);
	        }
	        else if (e.getKeyCode() == KeyEvent.VK_P)
	        {
	        	if (MainClass.paused)
	        		MainClass.paused = false;
	        	else if (!MainClass.paused)
	        		MainClass.paused = true;
	        }
	        else if (e.getKeyCode() == KeyEvent.VK_SLASH)
	        {
	        	if (main.hmenu.isActive())
	        	{
	        		main.hmenu.setActive(false);
	        	}
	        	else
	        	{
	        		MainClass.paused = true;
	        		main.hmenu.setActive(true);
	        	}
	        }
	        else if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
	        {
	        	main.cmenu.setActive(false);
	        	main.dmenu.setActive(false);
	        	main.designation.setDesignating(false);
	        	main.designation.setFollowing(false);
	        	main.designation.setDesignationType(tileType.Space);
	        	main.cursor.setObjectType(null);
	        	main.cursor.setActive(false);
	        	main.cumenu.setActive(false);
	        	main.keymenu.setActive(true);
	        	for (Person i:main.people.people) i.setSelected(false);
	        }
	    }
	    
	    
	    public void keyReleased (KeyEvent e)
	    {
	    	 if (e.getKeyCode() == KeyEvent.VK_SPACE)
	         {
	         	main.cursor.setAccelerated(false);
	         }
	    }
	    
	    public void keyTyped(KeyEvent e)
	    {
	    }
	    
	    private void dmenuAction()
	    {
	    	if (main.designation.isDesignating())
    		{
    			main.designation.setEndX(main.cursor.getX());
        		main.designation.setEndY(main.cursor.getY());
        		main.designation.setFollowing(false);
        		main.grid.setTileDesig(main.designation, main.designation.type);
    		}
    		else
    		{
    			main.designation.setDesignating(true);
    			main.designation.setFollowing(true);
    			main.designation.setStartX(main.cursor.getX());
    			main.designation.setStartY(main.cursor.getY());
    			main.cmenu.setActive(false);
    		}
	    }
	    
	    private void cmenuAction()
	    {
	    	if(main.cursor.getObjectType()!=null)
	    	{
		    	if(Resources.getResources() >= main.cursor.getObjectType().getValue() && main.grid.tiles[main.cursor.getX()/10][main.cursor.getY()/10].isOccupied() == false && main.cursor.getObjectType() == ObjectType.Door)
	    		{
	    			if (main.grid.tiles[main.cursor.getX()/10][main.cursor.getY()/10].getType().getSolid() == true)
	        		{
	        			main.grid.tiles[main.cursor.getX()/10][main.cursor.getY()/10].settype(tileType.Interior);
	        		}
	    			main.objectmanager.objects[ObjectManager.numObjects] = main.cursor.createObject();
	        		ObjectManager.numObjects++;
	   			}
		    	else if(Resources.getResources() >= main.cursor.getObjectType().getValue() && main.grid.tiles[main.cursor.getX()/10][main.cursor.getY()/10].isOccupied() == false && main.cursor.getObjectType() == ObjectType.Airlock)
	    		{
	    			if (main.grid.tiles[main.cursor.getX()/10][main.cursor.getY()/10].getType().getSolid() == true)
	        		{
	        			main.grid.tiles[main.cursor.getX()/10][main.cursor.getY()/10].settype(tileType.Interior);
	        		}
	    			main.objectmanager.objects[ObjectManager.numObjects] = main.cursor.createObject();
	        		ObjectManager.numObjects++;
	   			}
	    		else if (Resources.getResources() >= main.cursor.getObjectType().getValue() && main.grid.tiles[main.cursor.getX()/10][main.cursor.getY()/10].isOccupied() == false && main.grid.tiles[main.cursor.getX()/10][main.cursor.getY()/10].getType().getSolid() == false && main.cursor.getObjectType() != null)
	        	{
	        		main.grid.tiles[main.cursor.getX()/10][main.cursor.getY()/10].setOccupied(true);
	        		main.objectmanager.objects[ObjectManager.numObjects] = main.cursor.createObject();
	        		ObjectManager.numObjects++;
	        	}
	    	}
	    	else if (main.cursor.getObjectType() == null)
    		{
    			for (int i = 0;i<ObjectManager.numObjects;i++)
	        	{
	        		if (main.objectmanager.objects[i].getX() == main.cursor.getX() && main.objectmanager.objects[i].getY() == main.cursor.getY())
	        		{
	        			Resources.setResources(main.objectmanager.objects[i].type.getValue() + Resources.getResources());
	        			main.objectmanager.deleteObject(i);
	        			main.grid.tiles[main.cursor.getX()/10][main.cursor.getY()/10].setOccupied(false);
	        		}
	        	}
    		}
        	if(Resources.getResources() < main.cursor.objectType.getValue())
        		Resources.setRed();
	    }
	    
	    private void doorAction()
	    {
	    	for (int i = 0;i<ObjectManager.numObjects;i++)
        	{
        		if (main.objectmanager.objects[i].getX() == main.cursor.getX() && main.objectmanager.objects[i].getY() == main.cursor.getY() && main.objectmanager.objects[i].type == ObjectType.Door)
        			if (main.objectmanager.objects[i].blocksAir)
        			{
        				main.grid.tiles[main.cursor.getX()/10][main.cursor.getY()/10].changeType(tileType.Interior);
        				main.objectmanager.objects[i].setBlocksAir(false);
        				main.grid.tiles[main.cursor.getX()/10][main.cursor.getY()/10].setOccupied(false);
        			}
        			else if (!main.objectmanager.objects[i].blocksAir)
        			{
        				main.grid.tiles[main.cursor.getX() /10][main.cursor.getY()/10].changeType(tileType.Hull);
        				main.objectmanager.objects[i].setBlocksAir(true);
        				main.grid.tiles[main.cursor.getX()/10][main.cursor.getY()/10].setOccupied(true);
        			}
        	}
	    }
	    
	    private void personAction()
	    {
	    	for (Person i:main.people.people)
    		{
    			if (i.x == main.cursor.getX()/10 && i.y == main.cursor.getY()/10)
    			{
    				i.setSelected(true);
    			}
    			else if (i.isSelected())
    			{
    				i.setDestination(main.cursor.getX()/10, main.cursor.getY()/10);
    				i.userSetDestination = true;
    				i.setSelected(false);
    			}
    		}
	    }
}
